package cn.bigcontainer.alt.b2c.be.service.impl;

import cn.bigcontainer.alt.b2c.be.dto.B2cShangpinfenleiDto;
import cn.bigcontainer.alt.b2c.be.po.B2cShangpinfenlei;
import cn.bigcontainer.alt.common.util.WoKit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import cn.bigcontainer.alt.b2c.be.mapper.B2cShangpinfenleiMapper;
import cn.bigcontainer.alt.b2c.be.service.B2cShangpinfenleiService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品分类的Service接口实现类.
 *
 * @author jiaod
 * @date 2020-05-18
 */
@Service
@Transactional
public class B2cShangpinfenleiServiceImpl implements B2cShangpinfenleiService {

	@Resource
	private B2cShangpinfenleiMapper b2cShangpinfenleiMapper;

	@Override
	public List<B2cShangpinfenleiDto> getForm(String bh) {
		QueryWrapper<B2cShangpinfenlei> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("mbh",bh);
		List<B2cShangpinfenlei> list = b2cShangpinfenleiMapper.selectList(queryWrapper);
		return  WoKit.getDtos(B2cShangpinfenleiDto.class,list);
	}

	@Override
	public int saveForm(Map<String, Object> req) throws ParseException {
		B2cShangpinfenlei b2cShangpinfenlei = new B2cShangpinfenlei();
		b2cShangpinfenlei.setMid(Integer.parseInt(req.get("mid").toString()));
		b2cShangpinfenlei.setMmc((String)req.get("mmc"));
		b2cShangpinfenlei.setMbh((String) req.get("mbh"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		b2cShangpinfenlei.setMcjsj(sdf.parse((String) req.get("mcjsj")) );
		int i = b2cShangpinfenleiMapper.updateById(b2cShangpinfenlei);
		return i;
	}

	@Override
	public int delForm(Map<String, Object> req) {
		int mid = Integer.parseInt(req.get("mid").toString());
		int i = b2cShangpinfenleiMapper.deleteById(mid);
		return i;
	}

	@Override
	public List<B2cShangpinfenleiDto> getLever(String bh) {
		QueryWrapper<B2cShangpinfenlei> queryWrapper = new QueryWrapper<>();
		if (bh!=null){
			queryWrapper.likeRight("mbh",bh);
			queryWrapper.orderByAsc("mbh");
		}
		List<B2cShangpinfenlei> list = b2cShangpinfenleiMapper.selectList(queryWrapper);
		if (bh==null){
			list.removeIf(s->s.getMbh().length() !=4);
		}else {
			list.removeIf(s->s.getMbh().length() !=(bh.length()+4));
		}
		return  WoKit.getDtos(B2cShangpinfenleiDto.class,list);
	}

}
